;///// WickDD Installer     ////////////////////////////////////////////////////
; NSIS script to create installer for wickddup + C4D plugins
; detects C4D path by searching program files directory for CINEMA 4D.exe
;
; created by Rick Barrett
;//////////////////////////////////////////////////////////////////////////////

Name "WickDD Installer"
OutFile "wickddcli-win-1.0x.exe"
Caption "$(^Name)"
XPStyle on

; Convert .py to executable before compiling installer
!execute "make_exe.bat"

!include "Sections.nsh"
!include WinMessages.nsh

!include "FileFunc.nsh"
!insertmacro Locate

!define PATH_C4D $R0

;--------------------------------

!macro BIMAGE IMAGE
	Push $0
	GetTempFileName $0
	File /oname=$0 "${IMAGE}"
	SetBrandingImage $0
	Delete $0
	Pop $0
!macroend

;--------------------------------

; The default installation directory
InstallDir $PROGRAMFILES\wickdd\

SetFont "Arial" 10
AddBrandingImage top 50

ReserveFile "${NSISDIR}\Plugins\InstallOptions.dll"
ReserveFile "wickdd.ini"

;///// Define Pages       /////////////////////////////////////////////////////

PageEx license
   LicenseText "License"
   LicenseData LICENSE
PageExEnd

Page components C4DRegCheck

Page custom SetCustom ValidateCustom

Page instfiles

;///// Sections   /////////////////////////////////////////////////////////////

Section "WickDDUP Utility"

	SetOutPath $INSTDIR\wickddup
	File /r dist\*.*
	File ..\..\LICENSE
	File ..\..\license.txt
	File ..\ddup\*-icon.png
	File ..\ddup\upload-header.gif
	File ..\..\default\parameters.ini
	File ..\version.txt
	

    CreateDirectory "$SMPROGRAMS\WickDD"
    CreateShortCut "$SMPROGRAMS\WickDD\WickDD Uploader.lnk" "$INSTDIR\wickddup\wickddup.exe" "-g"

	; back up old value of .opt
	!define Index "Line${__LINE__}"
	  ReadRegStr $1 HKCR ".ddo" ""
	  StrCmp $1 "" "${Index}-NoBackup"
		StrCmp $1 "WickDDOptions" "${Index}-NoBackup"
		WriteRegStr HKCR ".ddo" "backup_val" $1
	"${Index}-NoBackup:"
	  WriteRegStr HKCR ".ddo" "" "WickDDOptions"
	  ReadRegStr $0 HKCR "WickDDOptions" ""
	  StrCmp $0 "" 0 "${Index}-Skip"
		WriteRegStr HKCR "WickDDOptions" "" "Wick Digital Dailies Options"
		WriteRegStr HKCR "WickDDOptions\shell" "" "open"
		WriteRegStr HKCR "WickDDOptions\DefaultIcon" "" "$INSTDIR\wickddup\wickddup.exe,0"
	"${Index}-Skip:"
	  WriteRegStr HKCR "WickDDOptions\shell\open" "" "Upload"
	  WriteRegStr HKCR "WickDDOptions\shell\open\command" "" \
		'$INSTDIR\wickddup\wickddup.exe "%1"'

	  System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
	!undef Index

SectionEnd


Section /o "CINEMA 4D Plugin" sectC4D
	
	SetOutPath ${PATH_C4D}\prefs
	File ..\..\default\parameters.ini 

	SetOutPath ${PATH_C4D}\plugins\wickdd
	File /r /x .svn /x *.c4d ..\c4d\*.*
	File ..\version.txt
	
SectionEnd


;///// Functions   ////////////////////////////////////////////////////////////

Function .onGUIinit
	!insertmacro BIMAGE "wickdd-logo.bmp"

	;Extract InstallOptions files
	;$PLUGINSDIR will automatically be removed when the installer closes

	InitPluginsDir
	File /oname=$PLUGINSDIR\wickdd.ini "wickdd.ini"

FunctionEnd

Function SetCustom

	Call GetInstallDir	
	WriteINIStr "$PLUGINSDIR\wickdd.ini" "Field 5" "ListItems" $R1

    InstallOptions::dialog "$PLUGINSDIR\wickdd.ini"

FunctionEnd

Function ValidateCustom

	ReadINIStr $0 "$PLUGINSDIR\wickdd.ini" "Settings" "State"
	StrCmp $0 0 validate  ; Next button?
	StrCmp $0 5 droplist ; "Show|Hide" drop-list?
	Abort ; Return to the page

	droplist:
		ReadINIStr ${PATH_C4D} "$PLUGINSDIR\wickdd.ini" "Field 5" "State"
		ReadINIStr $1 "$PLUGINSDIR\wickdd.ini" "Field 4" "HWND"
		SendMessage $1 ${WM_SETTEXT} 0 "STR:${PATH_C4D}"
		Abort ; Return to the page
		
	validate:
		ReadINIStr $INSTDIR "$PLUGINSDIR\wickdd.ini" "Field 2" "State"
		ReadINIStr ${PATH_C4D} "$PLUGINSDIR\wickdd.ini" "Field 4" "State"

FunctionEnd

Function C4DRegCheck
	ReadRegStr $0 HKCR ".c4d" ""
	StrCmp $0 "CINEMA 4D Document" setflag clearflag
	
	setflag:
		SectionSetFlags ${sectC4D} ${SF_SELECTED}
		Return

	clearflag:
		SectionSetFlags ${sectC4D} 0
		Return
		
FunctionEnd

Function C4DPathCheck
	
	; Copy found path into register 0
	StrCpy $R0 $R8
	StrCpy $R1 "$R1|$R8"

	; Confirm found path and stop locate function if ok
	;MessageBox MB_YESNO 'CINEMA 4D was found at the following location:$\n$R0$\n$\n \
	;	Do you wish to keep searching for additional installations?' IDYES +2
	;StrCpy $0 StopLocate

	Push $0

FunctionEnd

Function GetInstallDir

	!insertmacro SectionFlagIsSet ${sectC4D} ${SF_SELECTED} "GetC4DPath" "Exit"

	GetC4DPath:
	
		WriteINIStr "$PLUGINSDIR\wickdd.ini" "Field 3" "Flags" ""
		WriteINIStr "$PLUGINSDIR\wickdd.ini" "Field 4" "Flags" ""
		WriteINIStr "$PLUGINSDIR\wickdd.ini" "Field 5" "Flags" "NOTIFY"

		${Locate} "$PROGRAMFILES\MAXON" "/L=F /M=CINEMA?4D.exe" "C4DPathCheck"

		IfErrors 0 +2
			MessageBox MB_OK "CINEMA 4D could not be found. Please specify the install path manually." IDOK +2
			StrCpy $INSTDIR $R0\plugins\wickdd\
		
		Return
		
	Exit:
		WriteINIStr "$PLUGINSDIR\wickdd.ini" "Field 3" "Flags" "DISABLED"
		WriteINIStr "$PLUGINSDIR\wickdd.ini" "Field 4" "Flags" "DISABLED"
		WriteINIStr "$PLUGINSDIR\wickdd.ini" "Field 5" "Flags" "DISABLED"

FunctionEnd
